package com.example.dormitory_manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dormitory_manager.entity.Dormitory;
import com.example.dormitory_manager.entity.Moveout;
import com.example.dormitory_manager.entity.Student;
import com.example.dormitory_manager.form.SearchForm;
import com.example.dormitory_manager.mapper.DormitoryMapper;
import com.example.dormitory_manager.mapper.MoveoutMapper;
import com.example.dormitory_manager.mapper.StudentMapper;
import com.example.dormitory_manager.service.MoveoutService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dormitory_manager.utils.CommonUtil;
import com.example.dormitory_manager.utils.ResultVOUtil;
import com.example.dormitory_manager.vo.MoveoutVo;
import com.example.dormitory_manager.vo.PageVo;
import com.example.dormitory_manager.vo.ResultVo;
import com.example.dormitory_manager.vo.StudentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-10-02
 */
@Service
public class MoveoutServiceImpl extends ServiceImpl<MoveoutMapper, Moveout> implements MoveoutService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private DormitoryMapper dormitoryMapper;
    @Autowired
    private MoveoutMapper moveoutMapper;

    @Override
    public PageVo list(Integer page, Integer size) {
        Page<Student> studentPage = new Page<>(page, size);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", "入住");
        Page<Student> resultPage = this.studentMapper.selectPage(studentPage, queryWrapper);
        List<Student> studentList = resultPage.getRecords();

        //vo装换
        List<StudentVo> studentVosList = new ArrayList<>();
        for (Student student : studentList) {
            StudentVo studentVo = new StudentVo();
            //给对象复制值到另一个对象上
            BeanUtils.copyProperties(student, studentVo);
            Dormitory dormitory = this.dormitoryMapper.selectById(student.getDormitoryId());
            studentVo.setDormitoryName(dormitory.getName());
            studentVosList.add(studentVo);
        }

        PageVo pageVo = new PageVo();
        pageVo.setData(studentVosList);
        pageVo.setTotal(resultPage.getTotal());
        return pageVo;
    }

    @Override
    public PageVo search(SearchForm searchForm) {
        Page<Student> studentPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page<Student> resultPage = null;
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", "入住");
        if (searchForm.getValue().equals("")) {
            resultPage = this.studentMapper.selectPage(studentPage, queryWrapper);
        } else {
            queryWrapper.like(searchForm.getKey(), searchForm.getValue());
            resultPage = this.studentMapper.selectPage(studentPage, queryWrapper);
        }

        List<Student> studentList = resultPage.getRecords();

        //vo装换
        List<StudentVo> studentVosList = new ArrayList<>();
        for (Student student : studentList) {
            StudentVo studentVo = new StudentVo();
            //给对象复制值到另一个对象上
            BeanUtils.copyProperties(student, studentVo);
            Dormitory dormitory = this.dormitoryMapper.selectById(student.getDormitoryId());
            studentVo.setDormitoryName(dormitory.getName());
            studentVosList.add(studentVo);
        }

        PageVo pageVo = new PageVo();
        pageVo.setData(studentVosList);
        pageVo.setTotal(resultPage.getTotal());
        return pageVo;
    }

    @Override
    public ResultVo moveout(Integer id, String reason) {
        Moveout moveout = new Moveout();
        moveout.setStudentId(id);
        moveout.setReason(reason);
        Student student = this.studentMapper.selectById(id);
        moveout.setDormitoryId(student.getDormitoryId());
        moveout.setCreateDate(CommonUtil.createDate());
//        添加迁出记录
        int insert = this.moveoutMapper.insert(moveout);
        if (insert != 1) return ResultVOUtil.fail();
//        更新学生迁出状态
        student.setState("迁出");
        int update = this.studentMapper.updateById(student);
        if (update != 1) return ResultVOUtil.fail();
//        学生所在宿舍床位+1
        try {
            this.dormitoryMapper.addAvailible(student.getDormitoryId());
        } catch (Exception e) {
            return ResultVOUtil.fail();
        }

        return ResultVOUtil.success(null);
    }

    @Override
    public PageVo moveoutList(Integer page, Integer size) {
        Page<Moveout> moveoutPage = new Page<>(page, size);
        Page<Moveout> resultPage = this.moveoutMapper.selectPage(moveoutPage, null);
        PageVo pageVo = new PageVo();
        pageVo.setTotal(resultPage.getTotal());
        List<MoveoutVo> moveoutVoList = new ArrayList<>();
        for (Moveout moveout : resultPage.getRecords()) {
            MoveoutVo moveoutVo = new MoveoutVo();
            BeanUtils.copyProperties(moveout, moveoutVo);
            Student student = this.studentMapper.selectById(moveout.getStudentId());
            Dormitory dormitory = this.dormitoryMapper.selectById(moveout.getDormitoryId());
            moveoutVo.setStudentName(student.getName());
            moveoutVo.setDormitoryName(dormitory.getName());
            moveoutVoList.add(moveoutVo);
        }
        pageVo.setData(moveoutVoList);
        return pageVo;
    }
}
